#include "settings.h"
#include "QSettings"
#include "QMessageBox"
#include "QString"
Settings::Settings(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Settings)
{
    ui->setupUi(this);

	// Reload settings
    QSettings settings("../settings.ini", QSettings::IniFormat);
    ui->freqPauseH_spinBox->setValue(settings.value("freqPauseH",1).toInt());
    ui->freqPauseMin_spinBox->setValue(settings.value("freqPauseMin",30).toInt());
    ui->lengthPause_spinBox->setValue(settings.value("pauseLength",5).toInt());

    connect(ui->save_btn,SIGNAL(clicked()),this,SLOT(saveSettings()));
    connect(ui->back_btn,SIGNAL(clicked()),this,SIGNAL(backToHomeClicked()));
}

Settings::~Settings()
{
    delete ui;
}

void Settings::saveSettings() {
	// Check if settings are correct
    if (ui->freqPauseH_spinBox->value() == 0 && ui->freqPauseMin_spinBox->value() == 0) {
        QMessageBox::warning(this,QString::fromUtf8("La fréquence ne peut pas être nulle"),QString::fromUtf8("La fréquence de la pause ne peut pas être nulle"));
        return;
    }
    if (ui->lengthPause_spinBox->value() == 0) {
        QMessageBox::warning(this,QString::fromUtf8("La pause ne peut pas être nulle"),QString::fromUtf8("La pause ne peut pas être nulle"));
        return;
    }
	// Write settings
    QSettings settings("../settings.ini", QSettings::IniFormat);
    settings.setValue("freqPauseH", ui->freqPauseH_spinBox->value());
    settings.setValue("freqPauseMin", ui->freqPauseMin_spinBox->value());
    settings.setValue("pauseLength", ui->lengthPause_spinBox->value());
    emit settingsClicked();
    emit backToHomeClicked();
}
